/**
 * Tiem: 2023-2-9
 * FileName: SearchHeader
 * Editor: Haidong
 */

import React, { useRef } from 'react';
import { Form, Input, Space, Button, Select, DatePicker } from 'antd';
import _ from 'lodash';
import { SearchOutlined } from '@ant-design/icons';
import { getDateTime3 } from '@/utils/getDate';
import store from "@/store/store";
import moment from 'moment';

const { RangePicker } = DatePicker

const SearchHeader = ({ getValue, orderCode }) => {
    const formRef = useRef()
    const searchValue = store.getState().searchValue || {}
    const onFinish = (values) => {
      if (values.time && values.time.length) {
        values.beginTime = getDateTime3(values.time[0]) + ' 00:00:00'
        values.endTime = getDateTime3(values.time[1]) + ' 23:59:59'
      }
      delete (values.time)
      _.forIn(values, (v, k) => {
        if (!v) {
          delete (values[k])
        }
      })
      getValue(values)
    }
    const reset = () => {
      formRef.current.setFieldsValue({
        memberPhone:"",
        orderCode: '',
        hotelSalesReturnCode: '',
        time: []
      })
      getValue({})
    }
    return (
      <Form ref={formRef} onFinish={onFinish} layout="inline" initialValues={searchValue}>
        <Form.Item name="memberPhone">
          <Input style={{ width: 140 }} placeholder='用户手机号' suffix={<SearchOutlined />} defaultValue={searchValue.memberPhone} />
        </Form.Item>
        <Form.Item name="orderCode">
          <Input style={{ width: 140 }} placeholder='请输入订单号' suffix={<SearchOutlined />} defaultValue={searchValue.orderCode} />
        </Form.Item>
        <Form.Item name="hotelSalesReturnCode">
          <Input style={{ width: 140 }} placeholder='请输入退单号' suffix={<SearchOutlined />} defaultValue={searchValue.hotelSalesReturnCode} />
        </Form.Item>
        <Form.Item name="time">
          <RangePicker placeholder={['退单时间', '退单时间']}  defaultValue={searchValue.hasOwnProperty('beginTime') ? [moment(searchValue.beginTime, 'YYYY/MM/DD'), moment(searchValue.endTime, 'YYYY/MM/DD')] : []} />
        </Form.Item>
        <Form.Item>
          <Space>
            <Button htmlType="submit" type="primary">搜索</Button>
            <Button onClick={reset}>重置</Button>
          </Space>
        </Form.Item>
      </Form>
    );
  }

  export default SearchHeader